package UI;

import ADO.Logs;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

public class VLogin extends javax.swing.JDialog {

    /**
     * Creates new form VLogin
     */
    public VLogin(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    private VLogin() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jBIniciar = new javax.swing.JButton();
        jBSalir = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTxtUss = new javax.swing.JTextField();
        jPpass = new javax.swing.JPasswordField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                formKeyTyped(evt);
            }
        });

        jBIniciar.setText("Iniciar Sesion");
        jBIniciar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBIniciarActionPerformed(evt);
            }
        });
        jBIniciar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jBIniciarKeyPressed(evt);
            }
        });

        jBSalir.setText("Salir");
        jBSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBSalirActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Inicio de Sesión"));

        jLabel1.setText("Nombre de Usuario");

        jLabel2.setText("Contraseña");

        jTxtUss.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTxtUssActionPerformed(evt);
            }
        });

        jPpass.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jPpassKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPpass, javax.swing.GroupLayout.DEFAULT_SIZE, 122, Short.MAX_VALUE)
                    .addComponent(jTxtUss, javax.swing.GroupLayout.DEFAULT_SIZE, 122, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTxtUss, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jPpass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jBIniciar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jBSalir)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jBSalir)
                    .addComponent(jBIniciar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jBIniciarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBIniciarActionPerformed
    String usr = this.jTxtUss.getText().trim();
    String pass = this.jPpass.getText().trim();
    String MD5 = "";
    boolean b = true;
    try {
        MD5 = Entidades.Encriptacion.getMD5(pass);
    } catch (Exception ex) {
        Logger.getLogger(VLogin.class.getName()).log(Level.SEVERE, null, ex);
        b = false;
    }

    Soporte.GestorUsuario gu = new Soporte.GestorUsuario();
    if (gu.validarUsuario(usr, MD5) && b) {
        JOptionPane.showMessageDialog(rootPane, "USUARIO CORRECTO", "LOG IN CORRECTO", JOptionPane.INFORMATION_MESSAGE);
        Entidades.SingletonUsuario.getInstancia().setEstaLogueado(true);
        Entidades.SingletonUsuario.getInstancia().setUsuario(usr, pass);
        Logs l = new Logs();
        l.entrada(Logs.opciones.InicioSesion, Logs.ESCRITORIO);
        this.dispose();
    } else {
        JOptionPane.showMessageDialog(rootPane, "EL USUARIO O LA CONTRASEÑA SON INCORRECTAS", "ERROR DE LOG IN", JOptionPane.ERROR_MESSAGE);

    }
    //si logra iniciar sesion cambiar valor en singletonUsuario
}//GEN-LAST:event_jBIniciarActionPerformed

private void jBIniciarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jBIniciarKeyPressed
    if (evt.getKeyCode() == evt.VK_ENTER) {
        String usr = this.jTxtUss.getText().trim();
        String pass = this.jPpass.getText().trim();
        String MD5 = "";
        boolean b = true;
        try {
            MD5 = Entidades.Encriptacion.getMD5(pass);
        } catch (Exception ex) {
            Logger.getLogger(VLogin.class.getName()).log(Level.SEVERE, null, ex);
            b = false;
        }

        Soporte.GestorUsuario gu = new Soporte.GestorUsuario();
        if (gu.validarUsuario(usr, MD5) && b) {
            JOptionPane.showMessageDialog(rootPane, "USUARIO CORRECTO", "LOG IN CORRECTO", JOptionPane.INFORMATION_MESSAGE);
            Entidades.SingletonUsuario.getInstancia().setEstaLogueado(true);
            Entidades.SingletonUsuario.getInstancia().setUsuario(usr, pass);
            Logs l = new Logs();
            l.entrada(Logs.opciones.InicioSesion, Logs.ESCRITORIO);
            this.dispose();
        } else {
            JOptionPane.showMessageDialog(rootPane, "EL USUARIO O LA CONTRASEÑA SON INCORRECTAS", "ERROR DE LOG IN", JOptionPane.ERROR_MESSAGE);

        }
    }

}//GEN-LAST:event_jBIniciarKeyPressed

private void jBSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBSalirActionPerformed
    this.dispose();
}//GEN-LAST:event_jBSalirActionPerformed

private void jTxtUssActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTxtUssActionPerformed
}//GEN-LAST:event_jTxtUssActionPerformed

    private void formKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_formKeyTyped
        // TODO add your handling code here:
    }//GEN-LAST:event_formKeyTyped

    private void jPpassKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jPpassKeyPressed
        if (evt.getKeyCode() == evt.VK_ENTER) {
            String usr = this.jTxtUss.getText().trim();
            String pass = this.jPpass.getText().trim();
            String MD5 = "";
            boolean b = true;
            try {
                MD5 = Entidades.Encriptacion.getMD5(pass);
            } catch (Exception ex) {
                Logger.getLogger(VLogin.class.getName()).log(Level.SEVERE, null, ex);
                b = false;
            }

            Soporte.GestorUsuario gu = new Soporte.GestorUsuario();
            if (gu.validarUsuario(usr, MD5) && b) {
                JOptionPane.showMessageDialog(rootPane, "USUARIO CORRECTO", "LOG IN CORRECTO", JOptionPane.INFORMATION_MESSAGE);
                Entidades.SingletonUsuario.getInstancia().setEstaLogueado(true);
                Entidades.SingletonUsuario.getInstancia().setUsuario(usr, pass);
                Logs l = new Logs();
                l.entrada(Logs.opciones.InicioSesion, Logs.ESCRITORIO);
                this.dispose();
            } else {
                JOptionPane.showMessageDialog(rootPane, "EL USUARIO O LA CONTRASEÑA SON INCORRECTAS", "ERROR DE LOG IN", JOptionPane.ERROR_MESSAGE);

            }
        }
    }//GEN-LAST:event_jPpassKeyPressed

    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(VLogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(VLogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(VLogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(VLogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new VLogin().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBIniciar;
    private javax.swing.JButton jBSalir;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPasswordField jPpass;
    private javax.swing.JTextField jTxtUss;
    // End of variables declaration//GEN-END:variables
}
